#ifndef __LIST_BASE_H__
#define __LIST_BASE_H__

#include <stdint.h>
#include "FreeRTOS.h"

#define LIST_TRAVERSE(_pList, _pData)  for (list_node_t *p = &(_pList)->ptHead; (p->ptNext != &(_pList)->ptHead ? ((_pData) = p->ptNext->pData, 1) : 0); p = p->ptNext)

// #define LIST_HEAP_MALLOC(ulSize)            malloc(ulSize)
// #define LIST_HEAP_FREE(p)                   free(p)
#define LIST_HEAP_MALLOC(ulSize)            pvPortMalloc(ulSize)
#define LIST_HEAP_FREE(p)                   vPortFree(p)

typedef int32_t (*fn_i32_pp)(const void *, const void *);

typedef struct list_node_t{
    void *pData;
    struct list_node_t *ptLast;
    struct list_node_t *ptNext;
} list_node_t;

typedef struct list_base_t{
    list_node_t ptHead;
    int32_t iListLen;
} list_base_t;

void list_init(list_base_t *ptListHead);
void list_add(list_base_t *ptListHead, void *pData);
void list_add_end(list_base_t *ptListHead, void *pData);
void list_insert(list_base_t *ptListHead, void *pData, fn_i32_pp fnCompare);
void list_delete_node(list_base_t *ptListHead, void *pData);
void list_delete_all_node(list_base_t *ptListHead);
void *list_pop(list_base_t *ptListHead);

list_node_t *list_get_next_node(list_node_t *node);
void *list_get_node_by_num(list_base_t *ptListHead, uint32_t uNum);

#endif // __LIST_BASE_H__
